package com.morningstar.system.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.morningstar.common.result.ServiceCheckResult;
import com.morningstar.system.dao.entity.SysDept;
import com.morningstar.system.dao.mapper.SysDeptMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 部门管理 服务实现
 *
 * @author ruoyi
 */
@Slf4j
@Service
public class SysDeptService extends ServiceImpl<SysDeptMapper, SysDept> {
    /**
     * 添加或修改部门管理 校验逻辑
     *
     * @param sysDept 部门管理
     * @return 结果
     */
    public ServiceCheckResult checkAddAndUpdate(SysDept sysDept) {
        LambdaQueryWrapper<SysDept> checkDeptNameWrapper = new LambdaQueryWrapper<SysDept>()
                .eq(SysDept::getDeptName, sysDept.getDeptName())
                .ne(sysDept.getDeptId() != null, SysDept::getDeptId, sysDept.getDeptId());
        if (this.count(checkDeptNameWrapper) > 0) {
            return new ServiceCheckResult(true, "部门名称已存在");
        }
        return new ServiceCheckResult(false);
    }
}
